package com.woniuxy.shop.service;

import java.util.List;

import com.woniuxy.shop.dao.GoodsDao;
import com.woniuxy.shop.entity.Goods;
import com.woniuxy.shop.entity.Order;
import com.woniuxy.shop.entity.PageBean;
import com.woniuxy.shop.exception.DaoException;
import com.woniuxy.shop.exception.SameGoodException;
import com.woniuxy.shop.exception.ServiceException;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class GoodsService {
	GoodsDao gd = new GoodsDao();
	
	public void add(Goods g) {
		//如何定义商品重复？如果类别和名称一样则判定重复
		Goods temp = gd.getGoodsByName(g.getName());
		if(temp!=null&&temp.getCategoryid()==g.getCategoryid()) {
			throw new SameGoodException("同一类别下商品名不能重复");
		}
		try {
			gd.add(g);
		} catch (DaoException e) {
			new ServiceException("sql执行异常",e);
		}
	}
	
	/**
	 * 分页查询指定页面的商品信息
	 * @param page
	 * @param g
	 * @return
	 */
	public PageBean<Goods> getPageBean(int page, Goods g,int pagesize){
		 PageBean<Goods> pb = new PageBean<>();
		 int totalNum = gd.getGoodsAsLimit(g);
		 List<Goods>  goods = gd.getGoodsByPage(page, pagesize, g);
		 pb.setList(goods);
		 pb.setNowNum(goods.size());
		 pb.setNowPage(page);
		 pb.setPageSize(1);
		 pb.setTotalPage(totalNum%pagesize==0?totalNum/pagesize:totalNum/pagesize+1);
		 pb.setTotalNum(totalNum);
		 return pb;
	}
	
	/**
	 * 删除指定ID商品信息
	 * @param id
	 */
	public void delete(int id) {
		try {
			gd.delete(id);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
	}
	
	/**
	 * 更新指定id商品的状态
	 * @param id
	 * @param type
	 */
	public void updataStatus(int id,String type) {
		try {
			gd.updateStatus(id,type);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
	}
	
	/**
	 * 查询指定id的商品信息
	 * @param id
	 * @return
	 */
	public Goods getGoods(int id) {
		Goods g;
		try {
			g=gd.getGoodsByID(id);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		return g;
	}
	
	public void updateGoods(Goods g) {
		if(g==null) {
			return;
		}
		Goods temp = gd.getGoodsByName(g.getName());
		if(temp!=null&&temp.getCategoryid()==g.getCategoryid()&&g.getId()!=temp.getId()) {
			throw new SameGoodException("同一类别下商品名不能重复");
		}
		try {
			gd.updateAll(g);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		
	}
	
	/**
	 * 获取所有热卖商品
	 * @return
	 */
	public List<Goods> getHotGoods(){
		List<Goods> hotGoods = null;
		try {
			hotGoods = gd.getHotGoods("是");
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		return hotGoods;
	}
	
	/**
	 * 获取最新商品
	 * @return
	 */
	public List<Goods> getNewGoods(){
		List<Goods> newGoods = null;
		try {
			newGoods = gd.getNewGoods("是");
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		return newGoods;
	}
	
	public List<Goods> getGoodsByCateGory(int catagoryid){
		List<Goods> goodes = null;
		try {
			goodes = gd.getGoodsByCateGory(catagoryid);
		} catch (DaoException e) {
			throw new ServiceException("sql执行异常",e);
		}
		return goodes;
	}
	
	
	
}
